package com.zhongshengnetwork.rightbe.game.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.Target;
import com.dd.CircularProgressButton;
import com.tencent.connect.common.Constants;
import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzoneShare;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.wang.avi.AVLoadingIndicatorView;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.MainActivity;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.BroadcastDefine;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.common.customview.ActionSheetDialog;
import com.zhongshengnetwork.rightbe.common.customview.CustomDialog;
import com.zhongshengnetwork.rightbe.common.customview.PublishSelectPicPopupWindow;
import com.zhongshengnetwork.rightbe.common.utils.ToastUtil;
import com.zhongshengnetwork.rightbe.game.model.GameCommonUtils;
import com.zhongshengnetwork.rightbe.game.model.WordModel;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.gsonmodel.GameTimeOutModel;
import com.zhongshengnetwork.rightbe.gsonmodel.GuessAnswerTipModel;
import com.zhongshengnetwork.rightbe.gsonmodel.GuessWordAnswerModel;
import com.zhongshengnetwork.rightbe.gsonmodel.HistorySubjectModel;
import com.zhongshengnetwork.rightbe.gsonmodel.WordUserinfoModel;
import com.zhongshengnetwork.rightbe.gsonmodel.WordinfoModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;
import com.zhongshengnetwork.rightbe.login.LoginActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class GuessWordActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener{
    private final static String url = "http://a.app.qq.com/o/simple.jsp?pkgname=com.zhongshengnetwork.rightbe";//Constant.HOST+"downapp.do?";
    private final static String content = "成语猜猜，好玩又益智，赶紧来体验吧~";
    private List<String> list;
    private PublishSelectPicPopupWindow menuWindow;

    private TopBarView topbar;
    private TextView guess_word_level;//第几关
    private ImageView guess_word_image;//图片

    private CircularProgressButton guess_word_button_tip;//提示
    private CircularProgressButton guess_word_button_help;//求助
    private CircularProgressButton guess_word_button_share;//分享

    private CircularProgressButton guess_word_answer_first;//答案一
    private CircularProgressButton guess_word_answer_second;
    private CircularProgressButton guess_word_answer_third;
    private CircularProgressButton guess_word_answer_fourth;

    private TextView guess_word_gold;


    private CircularProgressButton guess_word_firstline_1;
    private CircularProgressButton guess_word_firstline_2;
    private CircularProgressButton guess_word_firstline_3;
    private CircularProgressButton guess_word_firstline_4;
    private CircularProgressButton guess_word_firstline_5;
    private CircularProgressButton guess_word_firstline_6;
    private CircularProgressButton guess_word_firstline_7;
    private CircularProgressButton guess_word_firstline_8;

    private CircularProgressButton guess_word_secondline_1;
    private CircularProgressButton guess_word_secondline_2;
    private CircularProgressButton guess_word_secondline_3;
    private CircularProgressButton guess_word_secondline_4;
    private CircularProgressButton guess_word_secondline_5;
    private CircularProgressButton guess_word_secondline_6;
    private CircularProgressButton guess_word_secondline_7;
    private CircularProgressButton guess_word_secondline_8;

    private CircularProgressButton guess_word_thirdline_1;
    private CircularProgressButton guess_word_thirdline_2;
    private CircularProgressButton guess_word_thirdline_3;
    private CircularProgressButton guess_word_thirdline_4;
    private CircularProgressButton guess_word_thirdline_5;
    private CircularProgressButton guess_word_thirdline_6;
    private CircularProgressButton guess_word_thirdline_7;
    private CircularProgressButton guess_word_thirdline_8;

    private CircularProgressButton guess_word_fourthline_1;
    private CircularProgressButton guess_word_fourthline_2;
    private CircularProgressButton guess_word_fourthline_3;
    private CircularProgressButton guess_word_fourthline_4;
    private CircularProgressButton guess_word_fourthline_5;
    private CircularProgressButton guess_word_fourthline_6;
    private CircularProgressButton guess_word_fourthline_7;
    private CircularProgressButton guess_word_fourthline_8;

    private WordinfoModel wordinfoModel = null;
    private WordUserinfoModel wordUserinfoModel = null;
    private Bitmap placeImg = null;
    private List<WordModel> textList;
    private List<String> answerList;
    private boolean isGetTip = false;
    private boolean isChecking = false;

    private String shareUrl;
    private String shareTitle;
    private String shareDes;
    private int shareType;
    private String nextPicUrl;

    private CountDownTime mTime;
    private boolean isTimeOut = false;
    private AVLoadingIndicatorView avi;
    private RelativeLayout guess_word_content;
    private LinearLayout guess_word_firstline;
    private LinearLayout guess_word_secondline;
    private LinearLayout guess_word_thirdline;
    private LinearLayout guess_word_fourthline;

    //历史题目相关的
    private HistorySubjectModel historySubjectModel = null;
    private String subjectIndex;

    private MyIUiListener mIUiListener = new MyIUiListener();
    class MyIUiListener implements IUiListener {
        @Override
        public void onComplete(Object o) {
            Log.e("TAG","分享成功");
            getShareGold();
        }

        @Override
        public void onError(UiError uiError) {
            Log.e("TAG","分享出错");
        }

        @Override
        public void onCancel() {
            Log.e("TAG","分享取消");
        }

        @Override
        public void onWarning(int i) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Tencent.onActivityResultData(requestCode, resultCode, data, mIUiListener);
        if (requestCode == Constants.REQUEST_API) {
            if (resultCode == Constants.REQUEST_QQ_SHARE || resultCode == Constants.REQUEST_QZONE_SHARE || resultCode == Constants.REQUEST_OLD_SHARE) {
                Tencent.handleResultData(data, mIUiListener);
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guess_word);
        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.getRightView().setVisibility(View.GONE);
        topbar.setClickListener(this);
        subjectIndex = getIntent().getStringExtra("subjectIndex");
        if (!CommonUtils.isEmpty(subjectIndex)){
            historySubjectModel = (HistorySubjectModel)getIntent().getSerializableExtra("historySubjectModel");
        }
        topbar.getRightTextView().setVisibility(View.INVISIBLE);
        /*
        topbar.getRightTextView().setText("排行榜");
        topbar.getRightTextView().setTextSize(15);
        topbar.getRightTextView().setClickable(true);
        topbar.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtils.isEmpty(CustomApplication.loginModel.getToken()) || CustomApplication.isAutoLogin == true){
                    startActivity(new Intent(GuessWordActivity.this, LoginActivity.class));
                    finish();
                    return;
                }
                if (wordUserinfoModel != null){
                    Intent intent = new Intent(GuessWordActivity.this,GameRangeActivity.class);
                    intent.putExtra("appid",wordUserinfoModel.getAppid());
                    startActivity(intent);
                }

            }
        });
        */
        topbar.getRightView().setImageResource(R.drawable.more);
        topbar.getRightView().setVisibility(View.VISIBLE);
        topbar.getRightView().setClickable(true);
        topbar.getRightView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActionSheetDialog actionSheetDialog = new ActionSheetDialog(GuessWordActivity.this)
                        .builder()
                        .setTitle("请选择")
                        .setCancelable(false)
                        .setCanceledOnTouchOutside(true);
                actionSheetDialog.addSheetItem("排行榜", null, new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                        go2GameRange();
                    }
                });
                actionSheetDialog.addSheetItem("历史答题", null, new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                        go2History();
                    }
                });
                actionSheetDialog.show();
            }
        });
        if (historySubjectModel != null){
            topbar.getRightView().setVisibility(View.INVISIBLE);
        }


        avi = (AVLoadingIndicatorView)findViewById(R.id.avi);
        list = new ArrayList<String>();
        list.add("分享给微信好友");
        list.add("分享到微信朋友圈");
        list.add("分享给QQ好友");
        list.add("分享到QQ空间");
        //list.add("取消");
        initUI();
        getWordInfo();
        registerBoradcastReceiver();
    }

    private void go2GameRange(){
        if (CommonUtils.isEmpty(CustomApplication.loginModel.getToken()) || CustomApplication.isAutoLogin == true){
            startActivity(new Intent(GuessWordActivity.this, LoginActivity.class));
            finish();
            return;
        }
        if (wordUserinfoModel != null){
            Intent intent = new Intent(GuessWordActivity.this,GameRangeActivity.class);
            intent.putExtra("appid",wordUserinfoModel.getAppid());
            startActivity(intent);
        }
    }

    private void go2History(){
        if (CommonUtils.isEmpty(CustomApplication.loginModel.getToken()) || CustomApplication.isAutoLogin == true){
            startActivity(new Intent(GuessWordActivity.this, LoginActivity.class));
            finish();
            return;
        }
        if (wordUserinfoModel != null){
            Intent intent = new Intent(GuessWordActivity.this,HistorySubjectActivity.class);
            intent.putExtra("appid",wordUserinfoModel.getAppid());
            intent.putExtra("subjectName","成语猜猜");
            startActivity(intent);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
        stopTime();
    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        //Toast.makeText(LoginActivity.this, "你点击了左侧按钮", Toast.LENGTH_LONG).show();
        back();
    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

    //菜单点击
    public void onClickMenu(View view){
        switch (view.getId()){
            case R.id.guess_word_button_tip:
                if (historySubjectModel != null){
                    if (CommonUtils.isEmpty(wordinfoModel.getDetail())){
                        ToastUtil.show(GuessWordActivity.this,"暂无解析哦");
                    }else {
                        CustomDialog dialog = null;
                        final CustomDialog.Builder builder = new CustomDialog.Builder(GuessWordActivity.this);
                        builder.setTitle(wordinfoModel.getAnswer());
                        builder.setMessage(wordinfoModel.getDetail());
                        builder.setPositiveButton("知道了", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        dialog = builder.create();
                        dialog.show();
                    }
                }else {
                    getTip();
                }
                break;
            case R.id.guess_word_button_help:
                //share(view);
                signDo();
                break;
            case R.id.guess_word_button_share:
                share(view);
                break;
        }
    }

    //答案点击
    public void onClickAnswer(View view){
        if (historySubjectModel != null){
            return;
        }
        String text = "";
        switch (view.getId()){
            case R.id.guess_word_answer_first:
                text = answerList.get(0);
                answerList.set(0,"");
                guess_word_answer_first.setText("");
                break;
            case R.id.guess_word_answer_second:
                text = answerList.get(1);
                answerList.set(1,"");
                guess_word_answer_second.setText("");
                break;
            case R.id.guess_word_answer_third:
                text = answerList.get(2);
                answerList.set(2,"");
                guess_word_answer_third.setText("");
                break;
            case R.id.guess_word_answer_fourth:
                text = answerList.get(3);
                answerList.set(3,"");
                guess_word_answer_fourth.setText("");
                break;
        }
        if (text.length() > 0 && textList != null && textList.size() > 0){
            for (int i = 0; i < textList.size(); i++){
                WordModel model = textList.get(i);
                if (model.getSelect() == 1 && text.equals(model.getText())){
                    model.setSelect(0);
                    model.setVisible(View.VISIBLE);
                    setText(text,i/8+1,i%8+1, View.VISIBLE);
                    break;
                }
            }
        }
    }

    private void dealWithAnswer(int index){
        if (historySubjectModel != null){
            return;
        }
        String text = "";
        if (index >= answerList.size()){
            return;
        }
        switch (index){
            case 0:
                text = answerList.get(0);
                answerList.set(0,"");
                guess_word_answer_first.setText("");
                break;
            case 1:
                text = answerList.get(1);
                answerList.set(1,"");
                guess_word_answer_second.setText("");
                break;
            case 2:
                text = answerList.get(2);
                answerList.set(2,"");
                guess_word_answer_third.setText("");
                break;
            case 3:
                text = answerList.get(3);
                answerList.set(3,"");
                guess_word_answer_fourth.setText("");
                break;
        }
        if (text.length() > 0 && textList != null && textList.size() > 0){
            for (int i = 0; i < textList.size(); i++){
                WordModel model = textList.get(i);
                if (model.getSelect() == 1 && text.equals(model.getText())){
                    model.setSelect(0);
                    model.setVisible(View.VISIBLE);
                    setText(text,i/8+1,i%8+1, View.VISIBLE);
                    break;
                }
            }
        }
    }

    //文字点击
    public void onClickText(View view){
        if (historySubjectModel != null){
            return;
        }
        if (isChecking == true){
            return;
        }
        int i = 0;
        switch (view.getId()){
            case R.id.guess_word_firstline_1:
                i = 1;
                break;
            case R.id.guess_word_firstline_2:
                i = 2;
                break;
            case R.id.guess_word_firstline_3:
                i = 3;
                break;
            case R.id.guess_word_firstline_4:
                i = 4;
                break;
            case R.id.guess_word_firstline_5:
                i = 5;
                break;
            case R.id.guess_word_firstline_6:
                i = 6;
                break;
            case R.id.guess_word_firstline_7:
                i = 7;
                break;
            case R.id.guess_word_firstline_8:
                i = 8;
                break;

            case R.id.guess_word_secondline_1:
                i = 9;
                break;
            case R.id.guess_word_secondline_2:
                i = 10;
                break;
            case R.id.guess_word_secondline_3:
                i = 11;
                break;
            case R.id.guess_word_secondline_4:
                i = 12;
                break;
            case R.id.guess_word_secondline_5:
                i = 13;
                break;
            case R.id.guess_word_secondline_6:
                i = 14;
                break;
            case R.id.guess_word_secondline_7:
                i = 15;
                break;
            case R.id.guess_word_secondline_8:
                i = 16;
                break;

            case R.id.guess_word_thirdline_1:
                i = 17;
                break;
            case R.id.guess_word_thirdline_2:
                i = 18;
                break;
            case R.id.guess_word_thirdline_3:
                i = 19;
                break;
            case R.id.guess_word_thirdline_4:
                i = 20;
                break;
            case R.id.guess_word_thirdline_5:
                i = 21;
                break;
            case R.id.guess_word_thirdline_6:
                i = 22;
                break;
            case R.id.guess_word_thirdline_7:
                i = 23;
                break;
            case R.id.guess_word_thirdline_8:
                i = 24;
                break;

            case R.id.guess_word_fourthline_1:
                i = 25;
                break;
            case R.id.guess_word_fourthline_2:
                i = 26;
                break;
            case R.id.guess_word_fourthline_3:
                i = 27;
                break;
            case R.id.guess_word_fourthline_4:
                i = 28;
                break;
            case R.id.guess_word_fourthline_5:
                i = 29;
                break;
            case R.id.guess_word_fourthline_6:
                i = 30;
                break;
            case R.id.guess_word_fourthline_7:
                i = 31;
                break;
            case R.id.guess_word_fourthline_8:
                i = 32;
                break;
        }
        if (textList != null && textList.size() > 0){
            boolean isEmpty = false;
            WordModel model = textList.get(i-1);
            for (int j = 0; j < answerList.size(); j++){
                if (answerList.get(j).length() == 0){
                    isEmpty = true;
                    model.setSelect(1);
                    model.setVisible(View.INVISIBLE);
                    setText(model.getText(),(i-1)/8+1,(i-1)%8+1, View.INVISIBLE);
                    answerList.set(j,model.getText());
                    setAnswerText(model.getText(),j);
                    break;
                }
            }
            textList.set(i-1,model);
            if (isEmpty == true){
                checkAnswer();
            }

        }

    }

    private void initUI(){
        textList = new ArrayList<WordModel>();
        answerList = new ArrayList<String>();
        answerList.add(0,"");
        answerList.add(1,"");
        answerList.add(2,"");
        answerList.add(3,"");
        guess_word_content = (RelativeLayout) findViewById(R.id.guess_word_content);
        guess_word_level = (TextView) findViewById(R.id.guess_word_level);//第几关
        guess_word_image = (ImageView)findViewById(R.id.guess_word_image);//图片

        guess_word_button_tip = (CircularProgressButton) findViewById(R.id.guess_word_button_tip);//提示
        guess_word_button_tip.setBackgroundColor(getResources().getColor(R.color.limitBackgroundColor));
        guess_word_button_tip.setStrokeColor(getResources().getColor(R.color.limitBackgroundColor));
        guess_word_button_tip.setText("提示");
        if (historySubjectModel != null){
            guess_word_button_tip.setText("解析");
        }

        guess_word_button_help = (CircularProgressButton) findViewById(R.id.guess_word_button_help);//求助
        guess_word_button_help.setBackgroundColor(getResources().getColor(R.color.orangeColor));
        guess_word_button_help.setStrokeColor(getResources().getColor(R.color.orangeColor));
        guess_word_button_help.setText("金币");

        guess_word_button_share = (CircularProgressButton) findViewById(R.id.guess_word_button_share);//分享
        guess_word_button_share.setBackgroundColor(getResources().getColor(R.color.lightGreenColor));
        guess_word_button_share.setStrokeColor(getResources().getColor(R.color.lightGreenColor));
        guess_word_button_share.setText("分享");

        guess_word_answer_first = (CircularProgressButton) findViewById(R.id.guess_word_answer_first);//答案一
        guess_word_answer_first.setText(answerList.get(0));
        guess_word_answer_first.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_answer_first.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_answer_second = (CircularProgressButton) findViewById(R.id.guess_word_answer_second);
        guess_word_answer_second.setText(answerList.get(1));
        guess_word_answer_second.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_answer_second.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_answer_third = (CircularProgressButton) findViewById(R.id.guess_word_answer_third);
        guess_word_answer_third.setText(answerList.get(2));
        guess_word_answer_third.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_answer_third.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_answer_fourth = (CircularProgressButton) findViewById(R.id.guess_word_answer_fourth);
        guess_word_answer_fourth.setText(answerList.get(3));
        guess_word_answer_fourth.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_answer_fourth.setStrokeColor(getResources().getColor(R.color.wordBgColor));

        guess_word_gold = (TextView) findViewById(R.id.guess_word_gold);


        guess_word_firstline_1 = (CircularProgressButton) findViewById(R.id.guess_word_firstline_1);
        guess_word_firstline_2 = (CircularProgressButton) findViewById(R.id.guess_word_firstline_2);
        guess_word_firstline_3 = (CircularProgressButton) findViewById(R.id.guess_word_firstline_3);
        guess_word_firstline_4 = (CircularProgressButton) findViewById(R.id.guess_word_firstline_4);
        guess_word_firstline_5 = (CircularProgressButton) findViewById(R.id.guess_word_firstline_5);
        guess_word_firstline_6 = (CircularProgressButton) findViewById(R.id.guess_word_firstline_6);
        guess_word_firstline_7 = (CircularProgressButton) findViewById(R.id.guess_word_firstline_7);
        guess_word_firstline_8 = (CircularProgressButton) findViewById(R.id.guess_word_firstline_8);

        guess_word_secondline_1 = (CircularProgressButton) findViewById(R.id.guess_word_secondline_1);
        guess_word_secondline_2 = (CircularProgressButton) findViewById(R.id.guess_word_secondline_2);
        guess_word_secondline_3 = (CircularProgressButton) findViewById(R.id.guess_word_secondline_3);
        guess_word_secondline_4 = (CircularProgressButton) findViewById(R.id.guess_word_secondline_4);
        guess_word_secondline_5 = (CircularProgressButton) findViewById(R.id.guess_word_secondline_5);
        guess_word_secondline_6 = (CircularProgressButton) findViewById(R.id.guess_word_secondline_6);
        guess_word_secondline_7 = (CircularProgressButton) findViewById(R.id.guess_word_secondline_7);
        guess_word_secondline_8 = (CircularProgressButton) findViewById(R.id.guess_word_secondline_8);

        guess_word_thirdline_1 = (CircularProgressButton) findViewById(R.id.guess_word_thirdline_1);
        guess_word_thirdline_2 = (CircularProgressButton) findViewById(R.id.guess_word_thirdline_2);
        guess_word_thirdline_3 = (CircularProgressButton) findViewById(R.id.guess_word_thirdline_3);
        guess_word_thirdline_4 = (CircularProgressButton) findViewById(R.id.guess_word_thirdline_4);
        guess_word_thirdline_5 = (CircularProgressButton) findViewById(R.id.guess_word_thirdline_5);
        guess_word_thirdline_6 = (CircularProgressButton) findViewById(R.id.guess_word_thirdline_6);
        guess_word_thirdline_7 = (CircularProgressButton) findViewById(R.id.guess_word_thirdline_7);
        guess_word_thirdline_8 = (CircularProgressButton) findViewById(R.id.guess_word_thirdline_8);

        guess_word_fourthline_1 = (CircularProgressButton) findViewById(R.id.guess_word_fourthline_1);
        guess_word_fourthline_2 = (CircularProgressButton) findViewById(R.id.guess_word_fourthline_2);
        guess_word_fourthline_3 = (CircularProgressButton) findViewById(R.id.guess_word_fourthline_3);
        guess_word_fourthline_4 = (CircularProgressButton) findViewById(R.id.guess_word_fourthline_4);
        guess_word_fourthline_5 = (CircularProgressButton) findViewById(R.id.guess_word_fourthline_5);
        guess_word_fourthline_6 = (CircularProgressButton) findViewById(R.id.guess_word_fourthline_6);
        guess_word_fourthline_7 = (CircularProgressButton) findViewById(R.id.guess_word_fourthline_7);
        guess_word_fourthline_8 = (CircularProgressButton) findViewById(R.id.guess_word_fourthline_8);

        guess_word_firstline_1.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_firstline_1.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_firstline_2.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_firstline_2.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_firstline_3.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_firstline_3.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_firstline_4.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_firstline_4.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_firstline_5.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_firstline_5.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_firstline_6.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_firstline_6.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_firstline_7.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_firstline_7.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_firstline_8.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_firstline_8.setStrokeColor(getResources().getColor(R.color.wordBgColor));

        guess_word_secondline_1.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_secondline_1.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_secondline_2.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_secondline_2.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_secondline_3.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_secondline_3.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_secondline_4.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_secondline_4.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_secondline_5.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_secondline_5.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_secondline_6.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_secondline_6.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_secondline_7.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_secondline_7.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_secondline_8.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_secondline_8.setStrokeColor(getResources().getColor(R.color.wordBgColor));

        guess_word_thirdline_1.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_thirdline_1.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_thirdline_2.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_thirdline_2.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_thirdline_3.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_thirdline_3.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_thirdline_4.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_thirdline_4.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_thirdline_5.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_thirdline_5.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_thirdline_6.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_thirdline_6.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_thirdline_7.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_thirdline_7.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_thirdline_8.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_thirdline_8.setStrokeColor(getResources().getColor(R.color.wordBgColor));

        guess_word_fourthline_1.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_fourthline_1.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_fourthline_2.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_fourthline_2.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_fourthline_3.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_fourthline_3.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_fourthline_4.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_fourthline_4.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_fourthline_5.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_fourthline_5.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_fourthline_6.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_fourthline_6.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_fourthline_7.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_fourthline_7.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        guess_word_fourthline_8.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        guess_word_fourthline_8.setStrokeColor(getResources().getColor(R.color.wordBgColor));

        guess_word_firstline = (LinearLayout) findViewById(R.id.guess_word_firstline);
        guess_word_secondline = (LinearLayout) findViewById(R.id.guess_word_secondline);
        guess_word_thirdline = (LinearLayout) findViewById(R.id.guess_word_thirdline);
        guess_word_fourthline = (LinearLayout) findViewById(R.id.guess_word_fourthline);

    }

    private void setText(String text, int line, int index, int visible){
        switch (index){
            case 1:
                if (line == 1){
                    guess_word_firstline_1.setText(text);
                    guess_word_firstline_1.setVisibility(visible);
                }else if (line == 2){
                    guess_word_secondline_1.setText(text);
                    guess_word_secondline_1.setVisibility(visible);
                }else if (line == 3){
                    guess_word_thirdline_1.setText(text);
                    guess_word_thirdline_1.setVisibility(visible);
                }else if (line == 4){
                    guess_word_fourthline_1.setText(text);
                    guess_word_fourthline_1.setVisibility(visible);
                }
                break;
            case 2:
                if (line == 1){
                    guess_word_firstline_2.setText(text);
                    guess_word_firstline_2.setVisibility(visible);
                }else if (line == 2){
                    guess_word_secondline_2.setText(text);
                    guess_word_secondline_2.setVisibility(visible);
                }else if (line == 3){
                    guess_word_thirdline_2.setText(text);
                    guess_word_thirdline_2.setVisibility(visible);
                }else if (line == 4){
                    guess_word_fourthline_2.setText(text);
                    guess_word_fourthline_2.setVisibility(visible);
                }
                break;
            case 3:
                if (line == 1){
                    guess_word_firstline_3.setText(text);
                    guess_word_firstline_3.setVisibility(visible);
                }else if (line == 2){
                    guess_word_secondline_3.setText(text);
                    guess_word_secondline_3.setVisibility(visible);
                }else if (line == 3){
                    guess_word_thirdline_3.setText(text);
                    guess_word_thirdline_3.setVisibility(visible);
                }else if (line == 4){
                    guess_word_fourthline_3.setText(text);
                    guess_word_fourthline_3.setVisibility(visible);
                }
                break;
            case 4:
                if (line == 1){
                    guess_word_firstline_4.setText(text);
                    guess_word_firstline_4.setVisibility(visible);
                }else if (line == 2){
                    guess_word_secondline_4.setText(text);
                    guess_word_secondline_4.setVisibility(visible);
                }else if (line == 3){
                    guess_word_thirdline_4.setText(text);
                    guess_word_thirdline_4.setVisibility(visible);
                }else if (line == 4){
                    guess_word_fourthline_4.setText(text);
                    guess_word_fourthline_4.setVisibility(visible);
                }
                break;
            case 5:
                if (line == 1){
                    guess_word_firstline_5.setText(text);
                    guess_word_firstline_5.setVisibility(visible);
                }else if (line == 2){
                    guess_word_secondline_5.setText(text);
                    guess_word_secondline_5.setVisibility(visible);
                }else if (line == 3){
                    guess_word_thirdline_5.setText(text);
                    guess_word_thirdline_5.setVisibility(visible);
                }else if (line == 4){
                    guess_word_fourthline_5.setText(text);
                    guess_word_fourthline_5.setVisibility(visible);
                }
                break;
            case 6:
                if (line == 1){
                    guess_word_firstline_6.setText(text);
                    guess_word_firstline_6.setVisibility(visible);
                }else if (line == 2){
                    guess_word_secondline_6.setText(text);
                    guess_word_secondline_6.setVisibility(visible);
                }else if (line == 3){
                    guess_word_thirdline_6.setText(text);
                    guess_word_thirdline_6.setVisibility(visible);
                }else if (line == 4){
                    guess_word_fourthline_6.setText(text);
                    guess_word_fourthline_6.setVisibility(visible);
                }
                break;
            case 7:
                if (line == 1){
                    guess_word_firstline_7.setText(text);
                    guess_word_firstline_7.setVisibility(visible);
                }else if (line == 2){
                    guess_word_secondline_7.setText(text);
                    guess_word_secondline_7.setVisibility(visible);
                }else if (line == 3){
                    guess_word_thirdline_7.setText(text);
                    guess_word_thirdline_7.setVisibility(visible);
                }else if (line == 4){
                    guess_word_fourthline_7.setText(text);
                    guess_word_fourthline_7.setVisibility(visible);
                }
                break;
            case 8:
                if (line == 1){
                    guess_word_firstline_8.setText(text);
                    guess_word_firstline_8.setVisibility(visible);
                }else if (line == 2){
                    guess_word_secondline_8.setText(text);
                    guess_word_secondline_8.setVisibility(visible);
                }else if (line == 3){
                    guess_word_thirdline_8.setText(text);
                    guess_word_thirdline_8.setVisibility(visible);
                }else if (line == 4){
                    guess_word_fourthline_8.setText(text);
                    guess_word_fourthline_8.setVisibility(visible);
                }
                break;
        }
    }

    private void setAnswerText(String text, int index){
        switch (index){
            case 0:
                guess_word_answer_first.setText(text);
                break;
            case 1:
                guess_word_answer_second.setText(text);
                break;
            case 2:
                guess_word_answer_third.setText(text);
                break;
            case 3:
                guess_word_answer_fourth.setText(text);
                break;
        }
    }

    //获取成语信息
    public void getWordInfo(){
        avi.show();
        answerList.set(0,"");
        answerList.set(1,"");
        answerList.set(2,"");
        answerList.set(3,"");
        guess_word_answer_first.setText("");
        guess_word_answer_second.setText("");
        guess_word_answer_third.setText("");
        guess_word_answer_fourth.setText("");
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        String url = "miniapp/appid/getuserwordinfo.do";
        if (historySubjectModel != null){
            map.put("id",(historySubjectModel.getIndex()-1)+"");
            url = "miniapp/"+"appid"+"/getsubjectcontent.do";
        }
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {// usersign
            @Override
            public void onSuccess(String data) {
                if (GuessWordActivity.this == null){
                    return;
                }
                avi.hide();
                Log.e("TAG","成语信息："+data+"\n");
                CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    wordinfoModel = GsonTools.getWordinfoModel(data);
                    wordUserinfoModel = GsonTools.getWordUserinfoModel(data);
                    Log.e("TAG","成语信息："+wordinfoModel.getId()+"\n");
                    if (wordUserinfoModel != null && wordinfoModel != null){
                        topbar.getTitleView().setText("【"+wordUserinfoModel.getLevelName()+"】");
                        wordUserinfoModel.setPlaylevel(wordUserinfoModel.getPlaylevel()+1);
                        //guess_word_level.setText("第 "+wordUserinfoModel.getPlaylevel()+" 关");
                        topbar.getTitleView().setText("第 "+wordUserinfoModel.getPlaylevel()+" 关");
                        guess_word_gold.setText(""+wordUserinfoModel.getGoldcount());

                        String nextUrl = "https://image.daydayrise.net/cy"+(wordinfoModel.getLevel()+2)+".jpg";
                        new getWordImageCacheAsyncTask(GuessWordActivity.this).execute(nextUrl);
                        if (placeImg != null && nextPicUrl.equals(wordinfoModel.getPicurl())){
                            guess_word_image.setImageBitmap(placeImg);
                            placeImg = null;
                            Log.e("TAG","直接设置图片");
                        }else {
                            if (GuessWordActivity.this != null){
                                Glide.with(GuessWordActivity.this).load(wordinfoModel.getPicurl()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(guess_word_image);
                            }
                        }
                        nextPicUrl = nextUrl;
                        String str = wordinfoModel.getAllwords().replace("[","");
                        str = str.replace("]","");
                        List<String> list = CommonUtils.stringToArrayList(str);
                        textList.clear();
                        if (list != null && list.size() > 0){
                            System.out.print("成语："+list.size());
                            for (int i = 0; i < list.size(); i++){
                                //setText(String text, int line, int index, int visible)
                                setText(list.get(i), (i/8)+1,(i%8)+1,View.VISIBLE);
                                WordModel model = new WordModel();
                                model.setText(list.get(i));
                                model.setSelect(0);
                                model.setVisible(View.VISIBLE);
                                textList.add(model);
                            }
                        }
                    }
                    //ToastUtil.show(GuessWordActivity.this,commonModel.getMsg());
                    Animation animation = AnimationUtils.loadAnimation(GuessWordActivity.this, R.anim.pushfromleft);
                    //guess_word_content.startAnimation(animation);
                    guess_word_firstline.startAnimation(animation);
                    guess_word_secondline.startAnimation(animation);
                    guess_word_thirdline.startAnimation(animation);
                    guess_word_fourthline.startAnimation(animation);
                    if (historySubjectModel != null){
                        String w = wordinfoModel.getAnswer();
                        answerList.set(0,w.substring(0,1));
                        answerList.set(1,w.substring(1,2));
                        answerList.set(2,w.substring(2,3));
                        answerList.set(3,w.substring(3,4));
                        guess_word_answer_first.setText(w.substring(0,1));
                        guess_word_answer_second.setText(w.substring(1,2));
                        guess_word_answer_third.setText(w.substring(2,3));
                        guess_word_answer_fourth.setText(w.substring(3,4));
                        guess_word_level.setText(subjectIndex);
                    }else {
                        startTime();
                    }
                }else {
                    //ToastUtil.show(GuessWordActivity.this,commonModel.getMsg());
                    CustomApplication.showTip(commonModel,GuessWordActivity.this);
                }
            }
            @Override
            public void onError(String msg) {
                avi.hide();
            }
        });
    }

    //获取提示
    public void getTip(){
        if (isGetTip == true){
            return;
        }
        if (wordinfoModel == null){
            return;
        }
        isGetTip = true;
        List<String>list = new ArrayList<String>();
        for (int i = 0; i < answerList.size(); i++){
            if (answerList.get(i).length() == 0){
                list.add(i+"");
            }
        }
        if (list.size() == 0){
            isGetTip = false;
            return;
        }
        //avi.show();
        int x=(int)(Math.random()*list.size());
        final int index = Integer.valueOf(list.get(x));

        String tip = GameCommonUtils.getTip(wordinfoModel.getAnswer(),index,wordUserinfoModel.getPlaylevel(),wordUserinfoModel.getGoldcount());
        if (CommonUtils.isEmpty(tip)){
            isGetTip = false;
            return;
        }else if (tip.length() > 1){
            isGetTip = false;
            ToastUtil.show(GuessWordActivity.this,tip);
            return;
        }else {
            wordUserinfoModel.setGoldcount(wordUserinfoModel.getGoldcount()-GameCommonUtils.getGoldByLevel(wordUserinfoModel.getPlaylevel()));
            guess_word_gold.setText(""+wordUserinfoModel.getGoldcount());
            answerList.set(index,tip);
            setAnswerText(tip,index);
            for (int ii = 0; ii < textList.size(); ii++){
                WordModel m = textList.get(ii);
                if (m.getSelect() == 0 && m.getText().equals(tip)){
                    setText(tip,ii/8+1,ii%8+1, View.INVISIBLE);
                    textList.set(ii,m);
                    break;
                }
            }
            checkAnswer();
            isGetTip = false;
        }

        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        map.put(APIKey.idKey,wordinfoModel.getId()+"");
        map.put("index",index+"");
        HttpsUtils.miniAppDo(map, "miniapp/appid/gettip.do", new HttpCallBack() {// usersign
            @Override
            public void onSuccess(String data) {
                Log.e("TAG","提示："+data+"\n");
                /*
                avi.hide();
                Log.e("TAG","提示："+data+"\n");
                CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){

                    GuessAnswerTipModel model = GsonTools.getGuessAnswerTipModel(data);
                    wordUserinfoModel.setGoldcount(model.getGold());
                    guess_word_gold.setText(""+wordUserinfoModel.getGoldcount());
                    answerList.set(index,model.getTip());
                    setAnswerText(model.getTip(),index);
                    for (int ii = 0; ii < textList.size(); ii++){
                        WordModel m = textList.get(ii);
                        if (m.getSelect() == 0 && m.getText().equals(model.getTip())){
                            setText(model.getTip(),ii/8+1,ii%8+1, View.INVISIBLE);
                            textList.set(ii,m);
                            break;
                        }
                    }
                    checkAnswer();

                }else {
                    //ToastUtil.show(GuessWordActivity.this,commonModel.getMsg());
                }
                isGetTip = false;
                */
            }
            @Override
            public void onError(String msg) {
                /*
                isGetTip = false;
                avi.hide();
                */
            }
        });
    }


    //检测答案
    public void checkAnswer(){
        Log.e("TAG","检测答案\n");
        int count = 0;
        for (int i = 0; i < answerList.size(); i++){
            if (answerList.get(i).length() > 0){
                count++;
            }
        }
        if (count == answerList.size()){
            if (isChecking == true){
                return;
            }
            isChecking = true;
            avi.show();
            String word = CommonUtils.listToString(answerList);
            word = word.replaceAll(",","");
            word = word.replaceAll(" ","");
            Log.e("TAG","检测答案:"+word);
            final HashMap<String, String> map = new HashMap<String, String>();
            map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
            map.put(APIKey.idKey,wordinfoModel.getId()+"");
            map.put("word",word);
            HttpsUtils.miniAppDo(map, "miniapp/appid/checkanswer.do", new HttpCallBack() {// usersign
                @Override
                public void onSuccess(String data) {
                    avi.hide();
                    Log.e("TAG",data);
                    CommonModel commonModel = GsonTools.getCommonModel(data);
                    if (commonModel.getFlag().equals("1")){
                        stopTime();
                        isChecking = false;
                        //ToastUtil.show(GuessWordActivity.this,commonModel.getMsg());
                        GuessWordAnswerModel model = GsonTools.getGuessWordAnswerModel(data);
                        CustomDialog dialog = null;
                        final CustomDialog.Builder builder = new CustomDialog.Builder(GuessWordActivity.this);
                        builder.setTitle(model.getWord());
                        builder.setMessage(model.getTips());
                        builder.setPositiveButton("下一关", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                getWordInfo();
                            }
                        });
                        dialog = builder.create();
                        dialog.show();

                    }else {

                        ToastUtil.show(GuessWordActivity.this,commonModel.getMsg());
                        new Handler().postDelayed(new Runnable(){
                            public void run() {
                                for (int i = 0; i < answerList.size(); i++){
                                    dealWithAnswer(i);
                                }
                                isChecking = false;
                                checkTimeOut();
                            }
                        }, 500);   //0.5秒
                    }
                }
                @Override
                public void onError(String msg) {
                    isChecking = false;
                    avi.hide();
                }
            });
        }

    }

    //分享求助
    public void getShare(){
        /*
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        HttpsUtils.miniAppDo(map, "miniapp/appid/getuserwordinfo.do", new HttpCallBack() {// usersign
            @Override
            public void onSuccess(String data) {
                Log.e("TAG",data);
                CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    ToastUtil.show(GuessWordActivity.this,commonModel.getMsg());
                }else {
                    ToastUtil.show(GuessWordActivity.this,commonModel.getMsg());
                }
            }
            @Override
            public void onError(String msg) {

            }
        });
        */
    }

    private void share(View v){
        /*
        menuWindow = new PublishSelectPicPopupWindow(GuessWordActivity.this,itemsOnClick,list);
        // 显示窗口
        menuWindow.showAtLocation(v,
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        */

        ActionSheetDialog actionSheetDialog = new ActionSheetDialog(GuessWordActivity.this)
                .builder()
                .setTitle("请选择")
                .setCancelable(false)
                .setCanceledOnTouchOutside(true);
        for (String s : list){
            actionSheetDialog.addSheetItem(s, null, new ActionSheetDialog.OnSheetItemClickListener() {
                @Override
                public void onClick(int which) {
                    if (which == 1){
                        //分享给微信好友
                        if (wordUserinfoModel != null){
                            shareTitle = "成语猜猜，好玩又益智，赶紧来体验吧~";
                            shareDes = "猜猜右图的是啥？";
                            shareType = 1;
                            shareUrl = url;
                            new getImageCacheAsyncTask(GuessWordActivity.this).execute(wordinfoModel.getPicurl());
                        }
                    }else if (which == 2){
                        //分享到朋友圈
                        if (wordUserinfoModel != null){
                            shareTitle = "【成语猜猜】猜猜右图的是啥？";
                            shareDes = "好玩又益智，赶紧来体验吧~";
                            shareType = 0;
                            shareUrl = url;
                            new getImageCacheAsyncTask(GuessWordActivity.this).execute(wordinfoModel.getPicurl());
                        }
                    }else if (which == 3){
                        //分享到qq
                        if (wordUserinfoModel != null){
                            shareTitle = "【成语猜猜】猜猜右图的是啥？";
                            shareDes = "好玩又益智，赶紧来体验吧~";
                            shareType = 0;
                            shareUrl = url;
                        }
                        shareToQQ(0,url,shareTitle,shareDes,wordinfoModel.getPicurl());
                    }else if (which == 4){
                        //qq空间
                        if (wordUserinfoModel != null){
                            shareTitle = "【成语猜猜】猜猜右图的是啥？";
                            shareDes = "好玩又益智，赶紧来体验吧~";
                            shareType = 0;
                            shareUrl = url;
                        }
                        shareToQQ(1,url,shareTitle,shareDes,wordinfoModel.getPicurl());
                    }
                }
            });
        }
        actionSheetDialog.show();
    }

    //分享到qq
    private Bundle params;
    private void shareToQQ(int type, String url,String title,String content,String picUrl){
        params = new Bundle();
        if (type == 0){//分享给QQ好友
            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
            params.putString(QQShare.SHARE_TO_QQ_TITLE, title);// 标题
            params.putString(QQShare.SHARE_TO_QQ_SUMMARY, content);// 摘要
            params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,url);// 内容地址
            params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,picUrl);// 网络图片地址
            params.putString(QQShare.SHARE_TO_QQ_EXT_INT, "");//其它附加功能
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    CustomApplication.mTencent.shareToQQ(GuessWordActivity.this, params, mIUiListener);
                }
            });
        }else {
            //分享到QQ空间
            params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE,QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
            params.putString(QzoneShare.SHARE_TO_QQ_TITLE, title);// 标题
            params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, content);// 摘要
            params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL,url);// 内容地址
            ArrayList<String> imgUrlList = new ArrayList<>();
            imgUrlList.add(picUrl);
            params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL,imgUrlList);// 图片地址
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    CustomApplication.mTencent.shareToQzone(GuessWordActivity.this, params, mIUiListener);
                }
            });
        }

    }

    // 为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            menuWindow.dismiss();
            switch (v.getId()) {
                case R.id.first_button://分享给微信好友
                    //shareToWX(1,url,content,"分享自微句");
                    if (wordUserinfoModel != null){
                        shareTitle = "成语猜猜，好玩又益智，赶紧来体验吧~";
                        shareDes = "猜猜右图的是啥？";
                        shareType = 1;
                        shareUrl = url;
                        new getImageCacheAsyncTask(GuessWordActivity.this).execute(wordinfoModel.getPicurl());
                    }
                    break;
                case R.id.second_button://分享到微信朋友圈
                    //shareToWX(0,url,content,"分享自微句");
                    if (wordUserinfoModel != null){
                        shareTitle = "【成语猜猜】猜猜右图的是啥？";
                        shareDes = "好玩又益智，赶紧来体验吧~";
                        shareType = 0;
                        shareUrl = url;
                        new getImageCacheAsyncTask(GuessWordActivity.this).execute(wordinfoModel.getPicurl());
                    }
                    break;
                case R.id.third_button:
                    break;
                default:
                    break;
            }

        }
    };

    /*
    //分享到微信
    private void shareToWX(int type,String url,String title,String content){
        //初始化一个WXWebpageObject对象，填写url
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = url;
        //用WXWebpageObject对象初始化一个WXMediaMessage对象，填写标题、描述
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = title;
        msg.description = content;
        Bitmap thumb = BitmapFactory.decodeResource(getResources(),R.mipmap.icon);
        msg.thumbData = CommonUtils.bitmapToByteArray(thumb,true);
        //构成一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message = msg;
        req.scene = type==0 ? SendMessageToWX.Req.WXSceneTimeline:SendMessageToWX.Req.WXSceneSession;
        CustomApplication.mWxApi.sendReq(req);

    }
    */


    //分享到微信
    private void shareToWX(int type,String url,String title,String content, Bitmap img){
        startTime();
        //初始化一个WXWebpageObject对象，填写url
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = url;
        //用WXWebpageObject对象初始化一个WXMediaMessage对象，填写标题、描述
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = title;
        msg.description = content;
        //Bitmap thumb = BitmapFactory.decodeResource(getResources(),R.mipmap.icon);
        msg.thumbData = CommonUtils.bitmapToByteArray(img,true);
        //构成一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message = msg;
        req.scene = type==0 ? SendMessageToWX.Req.WXSceneTimeline:SendMessageToWX.Req.WXSceneSession;
        CustomApplication.mWxApi.sendReq(req);
    }

    private class getImageCacheAsyncTask extends AsyncTask<String, Void, File> {
        private final Context context;

        public getImageCacheAsyncTask(Context context) {
            this.context = context;
        }

        @Override
        protected File doInBackground(String... params) {
            String imgUrl =  params[0];
            try {
                return Glide.with(context)
                        .load(imgUrl)
                        .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                        .get();
            } catch (Exception ex) {
                return null;
            }
        }

        @Override
        protected void onPostExecute(File result) {
            if (result == null) {
                return;
            }
            //此path就是对应文件的缓存路径
            String path = result.getPath();
            Log.e("path", path);
            Bitmap bmp= BitmapFactory.decodeFile(path);
            //img.setImageBitmap(bmp);
            bmp = CommonUtils.getZoomImage(bmp,28);
            shareToWX(shareType,shareUrl,shareTitle,shareDes,bmp);
        }
    }

    private class getWordImageCacheAsyncTask extends AsyncTask<String, Void, File> {
        private final Context context;

        public getWordImageCacheAsyncTask(Context context) {
            this.context = context;
        }

        @Override
        protected File doInBackground(String... params) {
            String imgUrl =  params[0];
            try {
                return Glide.with(context)
                        .load(imgUrl)
                        .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                        .get();
            } catch (Exception ex) {
                return null;
            }
        }

        @Override
        protected void onPostExecute(File result) {
            if (result == null) {
                return;
            }
            //此path就是对应文件的缓存路径
            String path = result.getPath();
            Log.e("path", path+"\n");
            placeImg = BitmapFactory.decodeFile(path);
        }
    }

    private void getShareGold(){
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        String url = "miniapp/"+wordUserinfoModel.getAppid()+"/usershare.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
            @Override
            public void onSuccess(String data) {
                final CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    wordUserinfoModel = GsonTools.getWordUserinfoModel(data);
                    new Handler().postDelayed(new Runnable(){
                        public void run() {
                            ToastUtil.show(GuessWordActivity.this,commonModel.getMsg());
                            guess_word_gold.setText(""+wordUserinfoModel.getGoldcount());
                            startTime();
                        }
                    }, 10);
                }
            }

            @Override
            public void onError(String msg) {

            }
        });
    }

    /**
     * 广播接收者
     * @author Gordon
     */
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(BroadcastDefine.WX_Share_Result)) {
                //微信分享成功
                getShareGold();
            }
        }
    };

    /**
     * 注册广播
     * @author Gordon
     */
    public void registerBoradcastReceiver(){
        IntentFilter myIntentFilter = new IntentFilter();
        //添加接收广播类型
        myIntentFilter.addAction(BroadcastDefine.WX_Share_Result);
        //注册广播
        registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    private void signDo(){
        if (wordUserinfoModel == null){
            return;
        }
        avi.show();
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey,CustomApplication.loginModel.getToken());
        String url = "miniapp/"+wordUserinfoModel.getAppid()+"/usersign.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {// usersign
            @Override
            public void onSuccess(String data) {
                avi.hide();
                Log.e("TAG",data);
                 final CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    wordUserinfoModel = GsonTools.getWordUserinfoModel(data);
                    new Handler().postDelayed(new Runnable(){
                        public void run() {
                            ToastUtil.show(GuessWordActivity.this,commonModel.getMsg());
                            guess_word_gold.setText(""+wordUserinfoModel.getGoldcount());
                        }
                    }, 10);
                }else {
                    ToastUtil.show(GuessWordActivity.this,"你已领取");
                }
            }
            @Override
            public void onError(String msg) {
                avi.hide();
            }
        });
    }

    /**
     * 第一种方法 使用android封装好的 CountDownTimer
     * 创建一个类继承 CountDownTimer
     */
    class CountDownTime extends CountDownTimer {

        //构造函数  第一个参数代表总的计时时长  第二个参数代表计时间隔  单位都是毫秒
        public CountDownTime(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long l) { //每计时一次回调一次该方法
            guess_word_level.setText("倒计时:"+l/1000+"秒");
        }

        @Override
        public void onFinish() { //计时结束回调该方法
            guess_word_level.setText("回答超时");
            isTimeOut = true;
            checkTimeOut();
        }
    }

    private void startTime(){
        if (historySubjectModel != null){
            return;
        }
        if (wordUserinfoModel == null){
            return;
        }
        if (mTime != null){
            mTime.cancel();
            mTime = null;
        }
        isTimeOut = false;
        mTime = new CountDownTime(wordinfoModel.getTimeout()*1000, 1000);//初始化对象
        mTime.start();
    }

    private void stopTime(){
        if (historySubjectModel != null){
            return;
        }
        if (wordUserinfoModel == null){
            return;
        }
        if (mTime != null){
            mTime.cancel();
            mTime = null;
        }
    }

    private void checkTimeOut(){
        if (isTimeOut == true && isChecking == false){
            final HashMap<String, String> map = new HashMap<String, String>();
            map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
            map.put(APIKey.idKey,wordinfoModel.getId()+"");
            String url = "miniapp/"+wordUserinfoModel.getAppid()+"/replytimeout.do";
            HttpsUtils.miniAppDo(map, url, new HttpCallBack() {// usersign
                @Override
                public void onSuccess(String data) {
                    Log.e("TAG","提示："+data+"\n");
                    CommonModel commonModel = GsonTools.getCommonModel(data);
                    if (commonModel.getFlag().equals("1")){
                        ToastUtil.show(GuessWordActivity.this,commonModel.getMsg());
                        startTime();
                        GameTimeOutModel model = GsonTools.getGameTimeOutModel(data);
                        wordUserinfoModel.setGoldcount(model.getGold());
                        guess_word_gold.setText(""+wordUserinfoModel.getGoldcount());
                        if (wordUserinfoModel.getGoldcount() < model.getRemovegold()){
                            back();
                        }
                    }else {
                        ToastUtil.show(GuessWordActivity.this,commonModel.getMsg());
                        back();
                    }
                }
                @Override
                public void onError(String msg) {

                }
            });
        }
    }

    private void back(){
        stopTime();
        finish();
    }

    //监听返回键(有软键盘的情况下想直接返回，需要拦截KeyEvent.ACTION_UP事件)
    @Override
    public boolean dispatchKeyEvent(KeyEvent event)
    {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP)
        {
            Log.e("TAG","点击了返回\n");
            back();
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopTime();
    }

    @Override
    protected void onResume() {
        super.onResume();
        startTime();
    }
}
